package cn.vc.content.service.video;

import cn.vc.content.entity.video.VideoHistory;
import cn.vc.content.mapper.video.VideoHistoryMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class VideoHistoryService {
    @Resource
    private VideoHistoryMapper videoHistoryMapper;


    /**
     * 添加视频历史记录
     * @param userId 对应用户ID
     * @param videoId 视频ID
     */
    public void records(Long userId, Long videoId) {
        VideoHistory videoHistory = this.selectMyVideoHistory(userId);
        if (videoHistory==null) {
            videoHistory = new VideoHistory();
            videoHistory.setUserId(userId);
            // 创建历史记录
            List<Long> videoIdList = new ArrayList<>();
            videoIdList.add(videoId);
            // 添加历史记录
            videoHistory.setVideoIdList(videoIdList);
            // 添加数量
            videoHistory.setVideoNum(1L);
            // 添加
            videoHistoryMapper.insert(videoHistory);
        } else {
            // 添加记录
            videoHistory.getVideoIdList().add(videoId);
            // 更新记录
            videoHistory.setVideoIdList(videoHistory.getVideoIdList());
            videoHistory.setVideoNum(videoHistory.getVideoNum()+1);
            // 添加
            videoHistoryMapper.updateById(videoHistory);
        }
    }


    /**
     * 查询视频历史记录
     */
    public VideoHistory selectMyVideoHistory(Long userId) {
        LambdaQueryWrapper<VideoHistory> lqw = new LambdaQueryWrapper<>();
        lqw.eq(VideoHistory::getUserId, userId);
        return videoHistoryMapper.selectOne(lqw);
    }
}
